# -*- coding: utf-8 -*-
import redis

from Lactationer_Slave import settings


class redisClient(object):
    # pool = redis.ConnectionPool(host='phonics.brightdot.cn',
    #                             port='35557',
    #                             password='@wecredo2016',
    #                             db='2')

    pool = redis.ConnectionPool(host='127.0.0.1',
                                port='6379',
                                password='',
                                db='2')
    r = redis.Redis(connection_pool=pool)

    # 请求失败的结果，保存下来，最后处理
    def saveFaild(self, url):
        redisClient.r.sadd('Lactationer:slave:failed', url)

    @property
    def getEffectiveUrl(self):
        exits = redisClient.r.srandmember('Lactationer:citys')
        if exits is not None:
            exits = redisClient.r.spop('Lactationer:citys:' + exits.decode())
            if exits is not None:
                # 城市下的列表有值,返回
                pass
                # return exits
            else:
                # 城市下的列表没有值，删除这个城市
                redisClient.r.srem('Lactationer:citys', exits)
                return self.getEffectiveUrl
        else:
            exits = redisClient.r.spop('Lactationer:slave:failed')
            # 城市都没有了，循环结束
            # return exits
            pass
        return exits.decode() if (exits is not None) else None
